import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'

import CategoriesEdit from '../views/CategoriesEdit.vue'
import CategoriesList from '../views/CategoriesList.vue'

import ItemsEdit from '../views/ItemsEdit.vue'
import ItemsList from '../views/ItemsList.vue'

import HeroEdit from '../views/HeroEdit.vue'
import HeroList from '../views/HeroList.vue'

import ArticleEdit from '../views/ArticleEdit.vue'
import ArticleList from '../views/ArticleList.vue'

import AdsEdit from '../views/AdsEdit.vue'
import AdsList from '../views/AdsList.vue'

import AdminUserEdit from '../views/AdminUserEdit.vue'
import AdminUserList from '../views/AdminUserList.vue'

import VideoEdit from '../views/VideoEdit.vue'
import VideoList from '../views/VideoList.vue'

import MatchEdit from '../views/MatchEdit.vue'
import MatchList from '../views/MatchList.vue'

import StrategyEdit from '../views/StrategyEdit.vue'
import StrategyList from '../views/StrategyList.vue'

Vue.use(VueRouter)

const routes = [
  //meta:{idPublic:true}表示允许公开访问，在下边路由守卫做处理
  { path: '/login', name: 'login', component: Login , meta: {isPublic: true} },
  {
    path: '/',
    name: 'main',
    component: Main,
    children: [
      { path: '/categories/create', component: CategoriesEdit },
      // props: true 可以将id在CategoriesEdit组建中用props接收一下直接用，而不必再使用URL传参(this.$router.params)
      { path: '/categories/edit/:id', component: CategoriesEdit, props: true },
      { path: '/categories/list', component: CategoriesList },

      { path: '/items/create', component: ItemsEdit },
      { path: '/items/edit/:id', component: ItemsEdit, props: true },
      { path: '/items/list', component: ItemsList },

      { path: '/hero/create', component: HeroEdit },
      { path: '/hero/edit/:id', component: HeroEdit, props: true },
      { path: '/hero/list', component: HeroList },

      { path: '/article/create', component: ArticleEdit },
      { path: '/article/edit/:id', component: ArticleEdit, props: true },
      { path: '/article/list', component: ArticleList },

      { path: '/ads/create', component: AdsEdit },
      { path: '/ads/edit/:id', component: AdsEdit, props: true },
      { path: '/ads/list', component: AdsList },

      { path: '/admin_users/create', component: AdminUserEdit },
      { path: '/admin_users/edit/:id', component: AdminUserEdit, props: true },
      { path: '/admin_users/list', component: AdminUserList },

      { path: '/video/create', component: VideoEdit },
      { path: '/video/edit/:id', component: VideoEdit, props: true },
      { path: '/video/list', component: VideoList },

      { path: '/match/create', component: MatchEdit },
      { path: '/match/edit/:id', component: MatchEdit, props: true },
      { path: '/match/list', component: MatchList },

      { path: '/strategy/create', component: StrategyEdit },
      { path: '/strategy/edit/:id', component: StrategyEdit, props: true },
      { path: '/strategy/list', component: StrategyList }
    ]
  },
]

const router = new VueRouter({
  routes
})

//路由前置守卫（后端利用token验证可以阻止查看列表那一半页面未，编辑页需要在前端进行路由守卫）
router.beforeEach((to, from, next) => {
  if(!to.meta.isPublic && !localStorage.token) {
    return next('/login')
  }
  next()
})

export default router
